package cn.buk.tms.eotms.controller;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.common.util.DateUtil;
import cn.buk.tms.eotms.dto.AttractionInfoDto;
import cn.buk.tms.eotms.dto.AttractionPriceDto;
import cn.buk.tms.eotms.dto.TicketOrderRequestDto;
import cn.buk.tms.eotms.dto.TicketOrderResponseDto;
import cn.buk.tms.eotms.sc.SearchFlightOrderCriteria;
import cn.buk.tms.eotms.service.AttractionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 景点
 * @author yfdai
 */
@RestController
public class AttractionsController extends BaseController {

    @Autowired
    public AttractionsController(AttractionService service) {
        this.service = service;
    }

    private final AttractionService service;


    @GetMapping("/attractions")
    public CommonDto<AttractionInfoDto> searchAttractions(HttpServletRequest request,
                                                          @RequestParam(value = "pageNo", defaultValue = "1") final int pageNo,
                                                          @RequestParam(value = "pageSize", defaultValue = "20") final int pageSzie
    ) {
        CommonSearchCriteria sc = new CommonSearchCriteria();
        sc.setPageNo(pageNo);
        sc.setPageSize(pageSzie);

        List<AttractionInfoDto> list = service.searchAttractions(getUserId(request), sc);

        CommonDto<AttractionInfoDto> dto = new CommonDto<>();
        dto.setDataList(list);
        dto.setPage(sc.getPage());

        return dto;
    }


    @GetMapping("/attraction/{id}")
    public AttractionInfoDto searchAttractionById(HttpServletRequest request,
                                                  @PathVariable("id") int id) {
        return service.getAttractionInfoById(getUserId(request), id);
    }


    @GetMapping("/attraction/{id}/prices")
    public CommonDto<AttractionPriceDto> searchAttractionPrice(HttpServletRequest request,
                                                               @PathVariable("id") final int attractionId,
                                                               @RequestParam(value = "pageNo", defaultValue = "1") final int pageNo,
                                                               @RequestParam(value = "pageSize", defaultValue = "20") final int pageSize
    ) {
        CommonSearchCriteria sc = new CommonSearchCriteria();
        sc.setPageNo(pageNo);
        sc.setPageSize(pageSize);

        List<AttractionPriceDto> datas = service.searchAttractionPrice(getUserId(request), attractionId, sc);

        CommonDto<AttractionPriceDto> commonDto = new CommonDto<>();
        commonDto.setDataList(datas);
        commonDto.setPage(sc.getPage());

        return commonDto;
    }

    /**
     * 创建景点门票订单
     *
     * @param request
     * @param dto
     * @return
     */
    @RequestMapping(value = "/attraction/ticket/order", consumes = "application/json", method = RequestMethod.POST)
    public JsonResult createAttractionTicketOrder(HttpServletRequest request,
                                                  @RequestBody TicketOrderRequestDto dto
    ) {
        return service.createAttractionTicketOrder(getUserId(request), dto);
    }

    /**
     * 查找门票订单
     */
    @GetMapping("/attraction/ticket/orders")
    public CommonDto<TicketOrderResponseDto> searchTicketOrders(HttpServletRequest request,
                                                                @ModelAttribute("sc") SearchFlightOrderCriteria sc
    ) {
        List<TicketOrderResponseDto> datas = service.searchTicketOrders(getUserId(request), sc);

        CommonDto<TicketOrderResponseDto> commonDto = new CommonDto<>();
        commonDto.setDataList(datas);
        commonDto.setPage(sc.getPage());

        return commonDto;
    }

    /**
     * 查找门票订单
     */
    @GetMapping("/attraction/ticket/order/{id}")
    public TicketOrderResponseDto getTicketOrder(HttpServletRequest request,
                                                 @PathVariable("id") int orderId
    ) {
        return service.getTicketOrder(getUserId(request), orderId);
    }

    /**
     * 取消订单
     *
     * @param request
     * @param orderId
     * @param remark
     * @return
     */
    @RequestMapping(value = "/attraction/ticket/order/{orderId}/status/canceled", method = RequestMethod.POST)
    public JsonResult cancelTicketOrder(HttpServletRequest request,
                                        @PathVariable("orderId") final int orderId,
                                        @RequestParam(value = "remark", required = false) String remark) {
        return service.cancelTicketOrder(getUserId(request), orderId, remark);
    }

    /**
     * 为门票订单付款
     */
    @PostMapping(value = "/attraction/ticket/order/{id}/pay")
    public JsonResult payTicketOrder(HttpServletRequest request,
                                     @PathVariable("id") int id
    ) {
        return service.payTicketOrder(getUserId(request), id);
    }

    /**
     * 通过上传excel文件，生成门票订单
     *
     * @param request
     * @param upfile
     * @return
     */
    @RequestMapping(value = "/attraction/ticket/order/upload", method = POST)
    public JsonResult uploadTicketOrders(HttpServletRequest request,
                                         MultipartFile upfile) {
        String fileName = getUserId(request) + "0" + DateUtil.getCurDateTimeString("yyyyMMddHHmmss");

        String filePath = System.getProperty("java.io.tmpdir") + "/" + fileName;
        File outputFile = new File(filePath);
        try {
            FileCopyUtils.copy(upfile.getBytes(), outputFile);
        } catch (IOException e) {
            e.printStackTrace();
            return JsonResult.createJsonResult(-1, "文件处理失败");
        }

        return service.importTicketOrderByExcel(getUserId(request), filePath);
    }
}
